#include <zenoui/draws/ButtonRectDraw.hpp>
#include <zenoui/check_gl.hpp>
#include <glm/ext.hpp>
#include <tuple>

namespace zenoui {

void ButtonRectDraw::paint(Rect const &rect, glm::vec4 color, bool pressed) {
    auto minirect = rect;
    minirect.extendMargin(-m_bound_size);

    auto colorDark = color * (1.0f - m_bound_shadow_strength);
    auto colorBright = 1.0f - (1.0f - color) * (1.0f - m_bound_shadow_strength);

    if (pressed) {
        std::swap(colorDark, colorBright);
    }

    glBegin(GL_QUAD_STRIP);
    glColor3fv(glm::value_ptr(colorBright));
    glVertex2f(rect.m_topleft.x + rect.m_size.x, rect.m_topleft.y);
    glVertex2f(minirect.m_topleft.x + minirect.m_size.x, minirect.m_topleft.y);
    glVertex2f(rect.m_topleft.x, rect.m_topleft.y);
    glVertex2f(minirect.m_topleft.x, minirect.m_topleft.y);
    glVertex2f(rect.m_topleft.x, rect.m_topleft.y + rect.m_size.y);
    glVertex2f(minirect.m_topleft.x, minirect.m_topleft.y + minirect.m_size.y);
    CHECK_GL(glEnd());

    glBegin(GL_QUAD_STRIP);
    glColor3fv(glm::value_ptr(colorDark));
    glVertex2f(rect.m_topleft.x, rect.m_topleft.y + rect.m_size.y);
    glVertex2f(minirect.m_topleft.x, minirect.m_topleft.y + minirect.m_size.y);
    glVertex2f(rect.m_topleft.x + rect.m_size.x, rect.m_topleft.y + rect.m_size.y);
    glVertex2f(minirect.m_topleft.x + minirect.m_size.x, minirect.m_topleft.y + minirect.m_size.y);
    glVertex2f(rect.m_topleft.x + rect.m_size.x, rect.m_topleft.y);
    glVertex2f(minirect.m_topleft.x + minirect.m_size.x, minirect.m_topleft.y);
    CHECK_GL(glEnd());

    glBegin(GL_QUADS);
    
    glColor3fv(glm::value_ptr(color));
    glVertex2f(minirect.m_topleft.x, minirect.m_topleft.y);
    glVertex2f(minirect.m_topleft.x + minirect.m_size.x, minirect.m_topleft.y);
    glVertex2f(minirect.m_topleft.x + minirect.m_size.x, minirect.m_topleft.y + minirect.m_size.y);
    glVertex2f(minirect.m_topleft.x, minirect.m_topleft.y + minirect.m_size.y);
    
    CHECK_GL(glEnd());
}

}
